<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class TimeControlModel
{
    public static function queryFirst($where,$field){
        return DB::table('t_time_control')->where($where)->first($field);
    }

    public static function add($param){
        return DB::table('t_time_control')->insert($param);
    }

    public static function queryLikeGet($param,$field){
        return DB::table('t_time_control')
            ->where('batch_id','like','%'.$param['batch_id'].'%')->get($field);
    }

    public static function queryParentCount($where,$param){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_time_control as t')
                ->leftJoin('t_admin as a','t.admin_id','=','a.id')
                ->where('t.sn_value','!=','')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('t.id');
        }
        return DB::table('t_time_control as t')
            ->leftJoin('t_admin as a','t.admin_id','=','a.id')
            ->where('t.sn_value','!=','')
            ->count('t.id');
    }

    public static function queryParentAll($where,$param,$page){
        $field = ['t.id','a.username','t.start_time','t.end_time','t.sn_value','t.create_time','t.day_type'];
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_time_control as t')
                ->leftJoin('t_admin as a','t.admin_id','=','a.id')
                ->where('t.sn_value','!=','')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field);
        }
        return DB::table('t_time_control as t')
            ->leftJoin('t_admin as a','t.admin_id','=','a.id')
            ->where('t.sn_value','!=','')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field);
    }

    public static function queryTeacherCount($where,$param){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_time_control as t')
                ->leftJoin('t_admin as a','t.admin_id','=','a.id')
                ->where('t.batch_id','!=','')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('t.id');
        }
        return DB::table('t_time_control as t')
            ->leftJoin('t_admin as a','t.admin_id','=','a.id')
            ->where('t.batch_id','!=','')
            ->count('t.id');
    }

    public static function queryTeacherAll($where,$param,$page){
        $field = ['t.id','a.username','t.start_time','t.end_time','t.batch_id','t.create_time','t.day_type'];
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_time_control as t')
                ->leftJoin('t_admin as a','t.admin_id','=','a.id')
                ->where('t.batch_id','!=','')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field);
        }
        return DB::table('t_time_control as t')
            ->leftJoin('t_admin as a','t.admin_id','=','a.id')
            ->where('t.batch_id','!=','')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field);
    }

    public static function queryLeftFirst($where,$field){
        return DB::table('t_time_control as t')
            ->leftJoin('t_admin as a','t.admin_id','=','a.id')->where($where)->first($field);
    }

    public static function edit($where,$param){
        return DB::table('t_time_control')->where($where)->update($param);
    }

    public static function delete($where){
        return DB::table('t_time_control')->where($where)->delete();
    }
}